package com.isb.emailclient;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.isb.emailclient.controller.Gmail;
import com.isb.emailclient.controller.IMail;
import com.isb.emailclient.model.SimpleMail;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends ActionBarActivity {

	private EditText editFrom;
	private EditText editTo;
	private EditText editContent;
	private Button btnSend;
	private Button btnCheckLogin;

	private String fromAddr;
	private String toAddr;
	private String content;

	private IMail gmail;
	
	private ProgressDialog progressDialog; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		// init variable
		gmail = new Gmail();

		editFrom = (EditText) this.findViewById(R.id.editForm);
		editTo = (EditText) this.findViewById(R.id.editTo);
		editContent = (EditText) this.findViewById(R.id.editContent);

		btnCheckLogin = (Button) this.findViewById(R.id.btnCheck);
		btnCheckLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				(new AsyncTask<Void, Void, Boolean>() {

					@Override
					protected void onPreExecute() {
						progressDialog = new ProgressDialog(LoginActivity.this);
						progressDialog.setMessage("loading...");
						progressDialog.show();
					}
					
					@Override
					protected Boolean doInBackground(Void... params) {
						fromAddr = editFrom.getText().toString();
						gmail.configMail(fromAddr, "GOO#include<1993>");
						
						gmail.getMailList("Inbox");
						
						return gmail.checkLoginInfo();
					}

					@Override
					protected void onPostExecute(Boolean result) {
						super.onPostExecute(result);
						if (result) {
							Toast.makeText(getApplicationContext(), "Login ok",
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(getApplicationContext(),
									"Login fail", Toast.LENGTH_SHORT).show();
						}
						
						progressDialog.dismiss();

					}

				}).execute();
			}
		});

		btnSend = (Button) this.findViewById(R.id.btnSend);
		btnSend.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				fromAddr = editFrom.getText().toString();
				toAddr = editTo.getText().toString();
				content = editContent.getText().toString();

				if (fromAddr.trim().length() > 0 && toAddr.trim().length() > 0
						&& content.trim().length() > 0) {

					gmail.configMail(fromAddr, "GOO#include<1993>");

					if (gmail.checkLoginInfo()) {

						(new AsyncTask<Void, Void, Void>() {

							@Override
							protected Void doInBackground(Void... params) {
								try {

									List<String> toAddrList = new ArrayList<String>();
									toAddrList.add(toAddr);
									SimpleMail sMail = new SimpleMail(fromAddr,
											toAddrList, content, "This is body");
									gmail.sendMail(sMail);
								} catch (Exception e) {
									e.printStackTrace();
								}
								return null;
							}

							@Override
							protected void onPostExecute(Void result) {
								super.onPostExecute(result);
								Toast.makeText(getApplicationContext(),
										"Finish!", Toast.LENGTH_SHORT).show();
							}
						}).execute();
					} else {
						Toast.makeText(getApplicationContext(), "Login error",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(getApplicationContext(),
							"Enter empty field", Toast.LENGTH_SHORT).show();
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
